﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace SoftballStats
{
    public partial class PlayerPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Season"] != null)
            {
                // get season
                SoftballStats_BL.Season thisSeason = Session["Season"] as SoftballStats_BL.Season;
                lblCurrentSeason.Text = thisSeason.Description + " " + thisSeason.Year;

                //get team
                SoftballStats_BL.Team myTeam = new SoftballStats_BL.Team();
                if (Session["Team"] != null)
                {
                    myTeam = Session["Team"] as SoftballStats_BL.Team;
                }

                if (Request.QueryString["PlayerID"] != null)
                {
                    SoftballStats_BL.Player selectedPlayer = SoftballStats_BL.Player.GetPlayerByID(Guid.Parse(Request.QueryString["PlayerID"]));
                    if (selectedPlayer.ID != Guid.Empty)
                    {
                        //fill player information, etc.
                        lblName.Text = selectedPlayer.FirstName + " " + selectedPlayer.LastName;
                        lblNumber.Text = "#" + selectedPlayer.Number.ToString();
                        lblPosition.Text = selectedPlayer.Position;
                        lblThrows.Text = selectedPlayer.Throws.ToString();
                        lblBats.Text = selectedPlayer.Bats.ToString();
                        if (myTeam.ID != Guid.Empty)
                        {
                            lblTeam.Text = myTeam.Name;
                        }
                        else
                        {
                            myTeam = SoftballStats_BL.Team.GetTeamByID(selectedPlayer.TeamID);
                            if (myTeam.ID != Guid.Empty)
                            {
                                lblTeam.Text = myTeam.Name;
                            }
                        }

                        //fill picture
                        imgLogo.ImageUrl = SoftballStats_BL.Player.GetPlayerImage(lblName.Text);

                        GetSeasonStats(selectedPlayer, thisSeason);
                    }
                    else
                    {
                        pnlPlayerInfo.Visible = false;
                    }
                }
                else
                {
                    pnlPlayerInfo.Visible = false;    
                }   
            }
            else
            {
                Response.Redirect("../SelectSeason.aspx");
            }

            //GetTeams();
        }

        private void GetSeasonStats(SoftballStats_BL.Player selectedPlayer, SoftballStats_BL.Season thisSeason)
        {
            SoftballStats_BL.PlayerGame playerSeasonStats = SoftballStats_BL.PlayerGame.GetPlayerSeasonStats(selectedPlayer.ID, selectedPlayer.FirstName, selectedPlayer.LastName, thisSeason.ID);

            HtmlTableRow tblRow = new HtmlTableRow();

            HtmlTableCell tblCell = new HtmlTableCell();
            Label lblSeason = new Label();
            lblSeason.Text = thisSeason.Description + " " + thisSeason.Year;
            tblCell.Controls.Add(lblSeason);
            tblRow.Cells.Add(tblCell);

            HtmlTableCell tblCell3 = new HtmlTableCell();
            Label lblHAB = new Label();
            lblHAB.Text = playerSeasonStats.Hits.ToString() + "/" + playerSeasonStats.AtBats.ToString();
            tblCell3.Controls.Add(lblHAB);
            tblRow.Cells.Add(tblCell3);

            HtmlTableCell tblCell4 = new HtmlTableCell();
            Label lblRuns = new Label();
            lblRuns.Text = playerSeasonStats.Runs.ToString();
            tblCell4.Controls.Add(lblRuns);
            tblRow.Cells.Add(tblCell4);

            HtmlTableCell tblCell5 = new HtmlTableCell();
            Label lblSingles = new Label();
            lblSingles.Text = playerSeasonStats.Singles.ToString();
            tblCell5.Controls.Add(lblSingles);
            tblRow.Cells.Add(tblCell5);

            HtmlTableCell tblCell13 = new HtmlTableCell();
            Label lblDoubles = new Label();
            lblDoubles.Text = playerSeasonStats.Doubles.ToString();
            tblCell13.Controls.Add(lblDoubles);
            tblRow.Cells.Add(tblCell13);

            HtmlTableCell tblCell14 = new HtmlTableCell();
            Label lblTriples = new Label();
            lblTriples.Text = playerSeasonStats.Triples.ToString();
            tblCell14.Controls.Add(lblTriples);
            tblRow.Cells.Add(tblCell14);

            HtmlTableCell tblCell12 = new HtmlTableCell();
            Label lblWalks = new Label();
            lblWalks.Text = playerSeasonStats.Walks.ToString();
            tblCell12.Controls.Add(lblWalks);
            tblRow.Cells.Add(tblCell12);

            HtmlTableCell tblCell6 = new HtmlTableCell();
            Label lblHomeRuns = new Label();
            lblHomeRuns.Text = playerSeasonStats.HomeRuns.ToString();
            tblCell6.Controls.Add(lblHomeRuns);
            tblRow.Cells.Add(tblCell6);

            HtmlTableCell tblCell7 = new HtmlTableCell();
            Label lblRBI = new Label();
            lblRBI.Text = playerSeasonStats.RunsBattedIn.ToString();
            tblCell7.Controls.Add(lblRBI);
            tblRow.Cells.Add(tblCell7);

            HtmlTableCell tblCell8 = new HtmlTableCell();
            Label lblTotalBases = new Label();
            lblTotalBases.Text = playerSeasonStats.TotalBases.ToString();
            tblCell8.Controls.Add(lblTotalBases);
            tblRow.Cells.Add(tblCell8);

            HtmlTableCell tblCell9 = new HtmlTableCell();
            Label lblAverage = new Label();
            lblAverage.Text = playerSeasonStats.Average.ToString();
            tblCell9.Controls.Add(lblAverage);
            tblRow.Cells.Add(tblCell9);

            HtmlTableCell tblCell10 = new HtmlTableCell();
            Label lblOBP = new Label();
            lblOBP.Text = playerSeasonStats.OnBasePercentage.ToString();
            tblCell10.Controls.Add(lblOBP);
            tblRow.Cells.Add(tblCell10);

            HtmlTableCell tblCell11 = new HtmlTableCell();
            Label lblSlugging = new Label();
            lblSlugging.Text = playerSeasonStats.Slugging.ToString();
            tblCell11.Controls.Add(lblSlugging);
            tblRow.Cells.Add(tblCell11);

            HtmlTableCell tblCell15 = new HtmlTableCell();
            Label lblOPS = new Label();
            lblOPS.Text = playerSeasonStats.OPS.ToString();
            tblCell15.Controls.Add(lblOPS);
            tblRow.Cells.Add(tblCell15);

            tblSeasonStats.Rows.Add(tblRow);
        }
    }
}